// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports


namespace 喷码数据生成工具
{
	public partial class ModifyAddress
	{
		
#region Default Instance
		
		private static ModifyAddress defaultInstance;
		
		/// <summary>
		/// Added by the VB.Net to C# Converter to support default instance behavour in C#
		/// </summary>
public static ModifyAddress Default
		{
			get
			{
				if (defaultInstance == null)
				{
					defaultInstance = new ModifyAddress();
					defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
				}
				
				return defaultInstance;
			}
			set
			{
				defaultInstance = value;
			}
		}
		
		static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
		{
			defaultInstance = null;
		}
		
#endregion
		const string HideChar = "********************************************************";
		bool SaveClose = true;
		public ModifyAddress()
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			
		}
		public ModifyAddress(bool mSaveClose)
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			SaveClose = mSaveClose;
		}
		DES jm = new DES();
		bool isJM = true;
		private const string jmCode = "sJ235689";
		
		public void ModifyAddress_Deactivate(object sender, System.EventArgs e)
		{
			if (isJM == false)
			{
				if (!(string.IsNullOrEmpty(Pwd.Text)))
				{
					string str = Pwd.Text;
					Pwd.Text = jm.Encrypt(str, jmCode);
					isJM = true;
				}
			}
		}
		public void ModifyAddress_Load(System.Object sender, System.EventArgs e)
		{
			DataBaseIP.Text = CommFunction.SaveData.DataBaseIP;
			if (CommFunction.SaveData.DataBasePort > 0)
			{
				Port.Text = System.Convert.ToString(CommFunction.SaveData.DataBasePort);
			}
			else
			{
				Port.Text = "1433";
			}
			DataBase.Text = CommFunction.SaveData.DataBase;
			UserID.Text = CommFunction.SaveData.DataBaseUserName;
			Pwd.Text = CommFunction.SaveData.DataBasePassword;
			Btn_Save.Enabled = false;
			CommFunction.ChangeButtonColor(this.Btn_Save);
			CommFunction.ChangeButtonColor(this.ConnectTest);
			DataBase.TextChanged += TextChange;
			DataBaseIP.TextChanged += TextChange;
			Port.TextChanged += TextChange;
			UserID.TextChanged += TextChange;
			Pwd.TextChanged += TextChange;
		}
		private void TextChange(object sender, EventArgs e)
		{
			Btn_Save.Enabled = false;
			if (((TextBox) sender).Name == "Port")
			{
				if (((TextBox) sender).Text == "" || ((TextBox) sender).Text == "0")
				{
					((TextBox) sender).Text = "1433";
				}
			}
		}
		public void ConnectTest_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				SqlDataBase sql = new SqlDataBase();
				sql.ConnectDataBase(DataBaseIP.Text, (int) Port.Text, UserID.Text, jm.Decrypt(Pwd.Text, jmCode), DataBase.Text);
				sql.Close();
				sql.Dispose();
				Btn_Save.Enabled = true;
				Interaction.MsgBox("链接成功,地址可用！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
			}
			catch (Exception ex)
			{
				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "链接失败");
			}
		}
		
		public void Btn_Save_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				CommFunction.SaveData.DataBase = DataBase.Text;
				CommFunction.SaveData.DataBaseIP = DataBaseIP.Text;
				CommFunction.SaveData.DataBasePassword = Pwd.Text;
				CommFunction.SaveData.DataBasePort = (int) Port.Text;
				CommFunction.SaveData.DataBaseUserName = UserID.Text;
				Form1.Default.SaveSetting();
				Interaction.MsgBox("设置保存成功！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
				if (SaveClose == true)
				{
					this.Close();
				}
			}
			catch (Exception ex)
			{
				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "错误");
			}
		}
		
		public void Pwd_Enter(object sender, System.EventArgs e)
		{
			if (!(string.IsNullOrEmpty(Pwd.Text)))
			{
				string str = Pwd.Text;
				Pwd.Text = jm.Decrypt(str, jmCode);
				isJM = false;
			}
		}
		
		public void Pwd_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) != 8)
			{
				if (Strings.Asc(e.KeyChar) < 46 || Strings.Asc(e.KeyChar) > 122)
				{
					this.Text = "不支持字符" + System.Convert.ToString(e.KeyChar) + "的输入！";
					e.Handled = true;
				}
			}
		}
		
		public void Pwd_TextChanged(System.Object sender, System.EventArgs e)
		{
			
		}
		
		public void Pwd_KeyUp(System.Object sender, System.Windows.Forms.KeyEventArgs e)
		{
			
		}
		
		public void Pwd_Leave(object sender, System.EventArgs e)
		{
			if (isJM == false)
			{
				if (!(string.IsNullOrEmpty(Pwd.Text)))
				{
					string str = Pwd.Text;
					Pwd.Text = jm.Encrypt(str, jmCode);
					isJM = true;
				}
			}
		}
		
		public void ModifyAddress_Leave(System.Object sender, System.EventArgs e)
		{
			if (isJM == false)
			{
				if (!(string.IsNullOrEmpty(Pwd.Text)))
				{
					string str = Pwd.Text;
					Pwd.Text = jm.Encrypt(str, jmCode);
					isJM = true;
				}
			}
		}
	}
}
